package cn.tedu.baidashi.service.Impl;

import cn.tedu.baidashi.exception.ServiceException;
import cn.tedu.baidashi.mapper.CategoryMapper;
import cn.tedu.baidashi.mapper.ItemsImgMapper;
import cn.tedu.baidashi.mapper.ItemsMapper;
import cn.tedu.baidashi.model.Items;
import cn.tedu.baidashi.model.ItemsImg;
import cn.tedu.baidashi.service.IItemsService;
import cn.tedu.baidashi.vo.ItemsInfoVO;
import cn.tedu.baidashi.vo.ItemsVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ItemsServiceImpl implements IItemsService {

    @Resource
    private ItemsMapper itemsMapper;
    @Resource
    private ItemsImgMapper itemsImgMapper;
    @Resource
    private CategoryMapper categoryMapper;

    /**
     * 根据商品id获得商品详情,前台
     * @param itemsId，商品id
     * @return itemsInfoVO，商品详情
     */
    @Override
    public ItemsInfoVO getItemsInfoById(Integer itemsId) {
        ItemsInfoVO itemsInfoVO=new ItemsInfoVO();
        //通过商品id获得商品详情
        Items items=itemsMapper.findItemsById(itemsId);
        //通过商品id获得商品图片
        ItemsImg itemsImg=itemsImgMapper.findItemsImgByItemsId(itemsId);
        //通过分类id获得分类名称
        String cateName=categoryMapper.findCategoryNameById(items.getCatId());
        //拼装itemsInfoVO
        itemsInfoVO.setItemsId(itemsId)
                .setCatId(items.getCatId())
                .setCatName(cateName)
                .setItemsImage(itemsImg.getUrl())
                .setItemsName(items.getItemsName())
                .setUnitPrice(items.getPrice())
                .setSynopsis(items.getSynopsis())
                .setItemsContent(items.getContent())
                .setSellCounts(items.getSellCounts())
                .setSpecification(items.getSpecification());
        
        return itemsInfoVO;
    }

    /**
     * 获得所有商品详情，后台
     * @return
     */
    @Override
    public PageInfo<Items> getAll(Integer pageNum, Integer pageSize) {

        //分页开始
        PageHelper.startPage(pageNum,pageSize);
        //查询所有商品
        List<Items> itemsList=itemsMapper.findAll();
        return new PageInfo<>(itemsList);
    }

    /**
     * 新增商品
     * @param itemsVO
     */
    @Transactional
    @Override
    public void addItems(ItemsVO itemsVO) {
        Items items=new Items();
        items.setItemsName(itemsVO.getItemsName())
                .setCatId(itemsVO.getCatId())
                .setSellCounts(0)
                .setSynopsis(itemsVO.getItemsSynopsis())
                .setContent(itemsVO.getItemsContent())
                .setPrice(itemsVO.getUnitPrice())
                .setStock(itemsVO.getStock())
                .setSpecification(itemsVO.getSpecification())
                .setItemsStatus(itemsVO.getItemsStatus())
                .setCreatedAt(LocalDateTime.now())
                .setUpdatedAt(LocalDateTime.now());
        int num=0;
        num=itemsMapper.addItems(items);
        if (num!=1){
            throw new ServiceException("数据库异常");
        }
        ItemsImg itemsImg=new ItemsImg();
        itemsImg.setUrl(itemsVO.getItemsImage())
                .setItemsId(items.getId())
                .setCreatedAt(LocalDateTime.now())
                .setUpdatedAt(LocalDateTime.now());

        num=itemsImgMapper.addItemsImage(itemsImg);
        if (num!=1){
            throw new ServiceException("数据库异常");
        }
    }

    /**
     * 修改商品时获得商品详情
     * @param itemsId
     * @return
     */
    @Override
    public ItemsVO getItemsVOById(Integer itemsId) {
        Items items=itemsMapper.findItemsById(itemsId);
        ItemsImg itemsImg=itemsImgMapper.findItemsImgByItemsId(itemsId);
        ItemsVO itemsVO=new ItemsVO();
        itemsVO.setItemsName(items.getItemsName())
                .setCatId(items.getCatId())
                .setItemsImage(itemsImg.getUrl())
                .setItemsSynopsis(items.getSynopsis())
                .setUnitPrice(items.getPrice())
                .setSpecification(items.getSpecification())
                .setStock(items.getStock())
                .setItemsStatus(items.getItemsStatus())
                .setItemsContent(items.getContent());
        return itemsVO;
    }

    /**
     * 删除商品
     * @param itemsStatus
     * @param itemsId
     */
    @Override
    public void deleteItemsById(Integer itemsStatus, Integer itemsId) {
        int num=itemsMapper.deleteItemsById(itemsStatus, itemsId);
        if (num!=1){
            throw new ServiceException("删除失败，数据库异常");
        }
    }

    @Override
    public void updateItemsById(ItemsVO itemsVO,Integer itemsId) {
        Items items=new Items();
        items.setId(itemsId).setItemsName(itemsVO.getItemsName())
                .setCatId(itemsVO.getCatId())
                .setSellCounts(0)
                .setSynopsis(itemsVO.getItemsSynopsis())
                .setContent(itemsVO.getItemsContent())
                .setPrice(itemsVO.getUnitPrice())
                .setStock(itemsVO.getStock())
                .setSpecification(itemsVO.getSpecification())
                .setItemsStatus(itemsVO.getItemsStatus())
                .setUpdatedAt(LocalDateTime.now());
        int num=0;
        num=itemsMapper.updateItemsById(items);
        if (num!=1){
            throw new ServiceException("数据库异常");
        }
        num=itemsImgMapper.updateItemsImage(itemsVO.getItemsImage(),LocalDateTime.now(),itemsId);
        if (num!=1){
            throw new ServiceException("数据库异常");
        }
    }


}
